<?php

/**
 * Folder form base class.
 *
 * @package    alika
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 16976 2009-04-04 12:47:44Z fabien $
 */
class BaseFolderForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                 => new sfWidgetFormInputHidden(),
      'tree_left'          => new sfWidgetFormInput(),
      'tree_right'         => new sfWidgetFormInput(),
      'tree_parent'        => new sfWidgetFormInput(),
      'scope'              => new sfWidgetFormInput(),
      'name'               => new sfWidgetFormInput(),
      'alias'              => new sfWidgetFormInput(),
      'description'        => new sfWidgetFormTextarea(),
      'created_at'         => new sfWidgetFormDateTime(),
      'user_id'            => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => true)),
      'view'               => new sfWidgetFormInput(),
      'count'              => new sfWidgetFormInput(),
      'custom_int'         => new sfWidgetFormInput(),
      'custom_int2'        => new sfWidgetFormInput(),
      'type_id'            => new sfWidgetFormPropelChoice(array('model' => 'TypeMaterial', 'add_empty' => true)),
      'photo_path'         => new sfWidgetFormInput(),
      'seo_title'          => new sfWidgetFormTextarea(),
      'seo_description'    => new sfWidgetFormTextarea(),
      'seo_keyword'        => new sfWidgetFormTextarea(),
      'is_last'            => new sfWidgetFormInputCheckbox(),
      'baner2_folder_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'Baner')),
      'folder2tag_list'    => new sfWidgetFormPropelChoiceMany(array('model' => 'Tag')),
      'mat2_folder_list'   => new sfWidgetFormPropelChoiceMany(array('model' => 'Material')),
    ));

    $this->setValidators(array(
      'id'                 => new sfValidatorPropelChoice(array('model' => 'Folder', 'column' => 'id', 'required' => false)),
      'tree_left'          => new sfValidatorInteger(),
      'tree_right'         => new sfValidatorInteger(),
      'tree_parent'        => new sfValidatorInteger(),
      'scope'              => new sfValidatorInteger(),
      'name'               => new sfValidatorString(array('max_length' => 250)),
      'alias'              => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'description'        => new sfValidatorString(array('required' => false)),
      'created_at'         => new sfValidatorDateTime(array('required' => false)),
      'user_id'            => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id', 'required' => false)),
      'view'               => new sfValidatorInteger(array('required' => false)),
      'count'              => new sfValidatorInteger(array('required' => false)),
      'custom_int'         => new sfValidatorInteger(array('required' => false)),
      'custom_int2'        => new sfValidatorInteger(array('required' => false)),
      'type_id'            => new sfValidatorPropelChoice(array('model' => 'TypeMaterial', 'column' => 'id', 'required' => false)),
      'photo_path'         => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'seo_title'          => new sfValidatorString(array('required' => false)),
      'seo_description'    => new sfValidatorString(array('required' => false)),
      'seo_keyword'        => new sfValidatorString(array('required' => false)),
      'is_last'            => new sfValidatorBoolean(array('required' => false)),
      'baner2_folder_list' => new sfValidatorPropelChoiceMany(array('model' => 'Baner', 'required' => false)),
      'folder2tag_list'    => new sfValidatorPropelChoiceMany(array('model' => 'Tag', 'required' => false)),
      'mat2_folder_list'   => new sfValidatorPropelChoiceMany(array('model' => 'Material', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('folder[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Folder';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['baner2_folder_list']))
    {
      $values = array();
      foreach ($this->object->getBaner2Folders() as $obj)
      {
        $values[] = $obj->getBanerId();
      }

      $this->setDefault('baner2_folder_list', $values);
    }

    if (isset($this->widgetSchema['folder2tag_list']))
    {
      $values = array();
      foreach ($this->object->getFolder2tags() as $obj)
      {
        $values[] = $obj->getTagId();
      }

      $this->setDefault('folder2tag_list', $values);
    }

    if (isset($this->widgetSchema['mat2_folder_list']))
    {
      $values = array();
      foreach ($this->object->getMat2Folders() as $obj)
      {
        $values[] = $obj->getMaterialId();
      }

      $this->setDefault('mat2_folder_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveBaner2FolderList($con);
    $this->saveFolder2tagList($con);
    $this->saveMat2FolderList($con);
  }

  public function saveBaner2FolderList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['baner2_folder_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(Baner2FolderPeer::FOLDER_ID, $this->object->getPrimaryKey());
    Baner2FolderPeer::doDelete($c, $con);

    $values = $this->getValue('baner2_folder_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Baner2Folder();
        $obj->setFolderId($this->object->getPrimaryKey());
        $obj->setBanerId($value);
        $obj->save();
      }
    }
  }

  public function saveFolder2tagList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['folder2tag_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(Folder2tagPeer::FOLDER_ID, $this->object->getPrimaryKey());
    Folder2tagPeer::doDelete($c, $con);

    $values = $this->getValue('folder2tag_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Folder2tag();
        $obj->setFolderId($this->object->getPrimaryKey());
        $obj->setTagId($value);
        $obj->save();
      }
    }
  }

  public function saveMat2FolderList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['mat2_folder_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(Mat2FolderPeer::FOLDER_ID, $this->object->getPrimaryKey());
    Mat2FolderPeer::doDelete($c, $con);

    $values = $this->getValue('mat2_folder_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Mat2Folder();
        $obj->setFolderId($this->object->getPrimaryKey());
        $obj->setMaterialId($value);
        $obj->save();
      }
    }
  }

}
